﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Vboot.App.K3CloudDataSend
{
    public class K3CloudLogin
    {
        //金蝶地址
        public static string CloudUrl = "http://192.168.5.108:8108/k3cloud/";    // "http://192.168.5.108:8108/k3cloud/";                 //本地："http://localhost/k3cloud/";
        //账套ID
        public static string ACID = "63b0535609eb4e";   //62e25e7bedd0a9 测试库账套ID    //63bd3b50d6fdf4  本地账套ID //63ddc9e8319821 230202 //63b0535609eb4e 正式  //64dcf18ff23422 IT专项测试_20230812
        //账号                     
        public string UserName { get; set; }
        //密码
        public string Password { get; set; }
        //语言
        public int LangurageID { get; set; }
            = 2052;

        private Microsoft.Extensions.Configuration.IConfiguration _IConfigurration;
        public K3CloudLogin(Microsoft.Extensions.Configuration.IConfiguration iconfiguration)
        {
            this._IConfigurration = iconfiguration;
        }
        public K3CloudLogin()
        {

        }

        public Task<string> CheckLogin()
        {
            //登录
            K3CloudLogin k3CloudLogin = new K3CloudLogin();
            //金蝶账号
            k3CloudLogin.UserName = "administrator";
            //密码
            k3CloudLogin.Password = "IT@2022";
            var LoginResult = k3CloudLogin.Login();
            //if (LoginResult != "SUCCESS")
            //    return "";
            return LoginResult;
        }

        /// 
        /// 登陆
        /// 
        public async Task<string> Login()
        {
            string result = "";
            HttpClient httpClient = new HttpClient();
            httpClient.Url = string.Concat(CloudUrl, "Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc");

            List<object> Parameters = new List<object>();
            Parameters.Add(ACID);//账套标示
            Parameters.Add(UserName);//用户名
            Parameters.Add(Password);//密码
            Parameters.Add(LangurageID);//2052代表中文
            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            var iResult = JObject.Parse( httpClient.AsyncRequest())["LoginResultType"].Value<int>();
            if (iResult == 1)
            {
                result = "SUCCESS";
            }
            return result;
        }   
    }
}
